﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ThreadStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            TimeoutTest4();
        }

        private static void NormalTest()
        {
            var thread = new Thread(() =>
            {
                Console.WriteLine(String.Format("线程{0}执行中", Thread.CurrentThread.ManagedThreadId));
            });

            thread.Start();
            thread.Join();

            Console.WriteLine(String.Format("线程{0}的状态：{1}", thread.ManagedThreadId, thread.ThreadState));
        }

        private static void AbortTest()
        {
            var thread = new Thread(() =>
            {
                Console.WriteLine(String.Format("线程{0}执行中", Thread.CurrentThread.ManagedThreadId));
                Thread.Sleep(100);
                Console.WriteLine(String.Format("线程{0}执行中", Thread.CurrentThread.ManagedThreadId));
            });

            thread.Start();
            Thread.Sleep(10);
            thread.Abort();
            thread.Join();

            Console.WriteLine(String.Format("线程{0}的状态：{1}", thread.ManagedThreadId, thread.ThreadState));
        }

        private static void ErrorTest()
        {
            var thread = new Thread(() =>
            {
                Console.WriteLine(String.Format("线程{0}执行中", Thread.CurrentThread.ManagedThreadId));
                throw new InvalidOperationException("去死吧");
                Console.WriteLine(String.Format("线程{0}执行中", Thread.CurrentThread.ManagedThreadId));
            });

            thread.Start();
            thread.Join();

            Console.WriteLine(String.Format("线程{0}的状态：{1}", thread.ManagedThreadId, thread.ThreadState));
        }

        private static void TimeoutTest1()
        {
            var cts = new CancellationTokenSource();

            var thread = new Thread(() =>
            {
                Console.WriteLine(String.Format("线程{0}执行中", Thread.CurrentThread.ManagedThreadId));
                Thread.Sleep(10000);
                Console.WriteLine(String.Format("线程{0}执行中", Thread.CurrentThread.ManagedThreadId));
            });

            cts.Token.Register(() =>
            {
                thread.Abort();
            });
            cts.CancelAfter(1000);

            thread.Start();
            thread.Join();

            Console.WriteLine(String.Format("线程{0}的状态：{1}", thread.ManagedThreadId, thread.ThreadState));
        }

        private static void TimeoutTest2()
        {
            var thread = new Thread(() =>
            {
                Console.WriteLine(String.Format("线程{0}执行中", Thread.CurrentThread.ManagedThreadId));
                Thread.Sleep(10000);
                Console.WriteLine(String.Format("线程{0}执行中", Thread.CurrentThread.ManagedThreadId));
            });

            thread.Start();
            thread.Join(1000);
            thread.Abort();

            Console.WriteLine(String.Format("线程{0}的状态：{1}", thread.ManagedThreadId, thread.ThreadState));
        }

        private static void TimeoutTest3()
        {
            var cts = new CancellationTokenSource();
            var task = new Task(() =>
            {
                while (true)
                {
                    cts.Token.ThrowIfCancellationRequested();

                    Console.WriteLine("xxxxxx");
                    Thread.Sleep(1000);
                }
            }, cts.Token);

            task.Start();

            cts.CancelAfter(5000);

            Console.ReadLine();
        }

        private static void TimeoutTest4()
        {
            var task = new Task(() =>
            {
                while (true)
                {
                    Console.WriteLine("xxxxxx");
                    Thread.Sleep(1000);
                }
            });

            task.Start();

            task.Wait(5000);
            task.();

            Console.ReadLine();
        }
    }
}
